package org.arnetminer.tapestry.dao;

import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.arnetminer.tapestry.entities.User;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

public class UserDAOImpl implements UserDAO {
    
    @Inject
    private Session session;
    
    public boolean isExist(User user){
    	Criteria criteria = session.createCriteria(User.class);
    	criteria.add(Expression.eq("usr", user.getUsr()));
    	List<User> results = criteria.list();
    	if (results.size() == 0) {
    		return false;
    	}
    	return true;
    }
    
    public void save(User user) {
    	session.persist(user);
    }
    
    public boolean login(User user){
    	Criteria criteria = session.createCriteria(User.class);
    	criteria.add(Expression.eq("usr", user.getUsr()));
    	criteria.add(Expression.eq("pwd", user.getPwd()));
    	List<User> results = criteria.list();
    	if (results.size() == 0) {
    		return false;
    	}
    	return true;
    }

}
